Trò chơi Angry Birds trong UNITY Engine
31.646 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3 using Assets.Scripts;
4
5 public class CameraMove : MonoBehaviour
6 {
7
8
9 // Update is called once per frame
10 void Update()
11 {
12 if (SlingShot.slingshotState == SlingshotState.Idle && GameManager.CurrentGameState == GameState.Playing)
13 {
14 //drag start
15 if (Input.GetMouseButtonDown(0))
16 {
17 timeDragStarted = Time.time;
18 dragSpeed = 0f;
19 previousPosition = Input.mousePosition;
20 }
21 //we calculate time difference in order for the following code
22 //NOT to run on single taps ;)
23 else if (Input.GetMouseButton(0) && Time.time - timeDragStarted > 0.05f)
24 {
25 //find the delta of this point with the previous frame
26 Vector3 input = Input.mousePosition;
27 float deltaX = (previousPosition.x - input.x) * dragSpeed;
28 float deltaY = (previousPosition.y - input.y) * dragSpeed;
29 //clamp the values so that we drag within limits
30 float newX = Mathf.Clamp(transform.position.x + deltaX, 0, 13.36336f);
31 float newY = Mathf.Clamp(transform.position.y + deltaY, 0, 2.715f);
32 //move camera
33 transform.position = new Vector3(
34 newX,
35 newY,
36 transform.position.z);
37
38 previousPosition = input;
39 //some small acceleration ;)
40 if(dragSpeed < 0.1f) dragSpeed += 0.002f;
41 }
42 }
43 }
44
45 private float dragSpeed = 0.01f;
46 private float timeDragStarted;
47 private Vector3 previousPosition = Vector3.zero;
48
49 public SlingShot SlingShot;
50 }
2 using System.Collections;
3 using Assets.Scripts;
4
5 public class CameraMove : MonoBehaviour
6 {
7
8
9 // Update is called once per frame
10 void Update()
11 {
12 if (SlingShot.slingshotState == SlingshotState.Idle && GameManager.CurrentGameState == GameState.Playing)
13 {
14 //drag start
15 if (Input.GetMouseButtonDown(0))
16 {
17 timeDragStarted = Time.time;
18 dragSpeed = 0f;
19 previousPosition = Input.mousePosition;
20 }
21 //we calculate time difference in order for the following code
22 //NOT to run on single taps ;)
23 else if (Input.GetMouseButton(0) && Time.time - timeDragStarted > 0.05f)
24 {
25 //find the delta of this point with the previous frame
26 Vector3 input = Input.mousePosition;
27 float deltaX = (previousPosition.x - input.x) * dragSpeed;
28 float deltaY = (previousPosition.y - input.y) * dragSpeed;
29 //clamp the values so that we drag within limits
30 float newX = Mathf.Clamp(transform.position.x + deltaX, 0, 13.36336f);
31 float newY = Mathf.Clamp(transform.position.y + deltaY, 0, 2.715f);
32 //move camera
33 transform.position = new Vector3(
34 newX,
35 newY,
36 transform.position.z);
37
38 previousPosition = input;
39 //some small acceleration ;)
40 if(dragSpeed < 0.1f) dragSpeed += 0.002f;
41 }
42 }
43 }
44
45 private float dragSpeed = 0.01f;
46 private float timeDragStarted;
47 private Vector3 previousPosition = Vector3.zero;
48
49 public SlingShot SlingShot;
50 }